import { useEffect, useState } from "react";
export default function useLocalStorageState<T>(key:string,defaultValue:T){
    const [state,setState] = useState<T|null>(defaultValue);
    useEffect(()=>{
        const localRes = localStorage.getItem(key);
        if(localRes!==null) setState( JSON.parse(localRes) as T)
    },[])
    function setLocalState(newState:T){
        localStorage.setItem(key,JSON.stringify(newState))
        setState(newState);
    }
    return [state,setLocalState] as const;
}